package provider.controller;

import entity.User;
import org.springframework.web.bind.annotation.*;
import vo.ResponseResult;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    private static Map<Integer, User> users=new HashMap<>();

    private static Integer id=1;

    @PostMapping
    public ResponseResult add1(User user){
        System.out.println("Provider.UserController.add1()");
        user.setId(id++);
        users.put(user.getId(),user);
        return ResponseResult.success();
    }

    @PostMapping("/add2")
    public ResponseResult add2(@RequestBody User user){
        System.out.println("Provider.UserController.add2()");
        user.setId(id++);
        users.put(user.getId(),user);
        return ResponseResult.success();
    }

    @PostMapping("/add3")
    public ResponseResult add3(User user){
        System.out.println("Provider.UserController.add3()");
        user.setId(id++);
        users.put(user.getId(),user);
        return ResponseResult.success();
    }

    @DeleteMapping("/{id}")
    public ResponseResult remove(@PathVariable("id") Integer id){
        System.out.println("Provider.UserController.remove()");
        users.remove(id);
        return ResponseResult.success();
    }

    @PutMapping
    public ResponseResult modify(User user){
        System.out.println("Provider.UserController.modify()");
        users.put(user.getId(),user);
        return ResponseResult.success();
    }

    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable("id") Integer id){
        System.out.println("Provider.UserController.findById()");

        if(id==2){
            int i=1/0;
        }

        User user=users.get(id);
        return ResponseResult.success(user);
    }

    @GetMapping
    public ResponseResult findAll(){
        System.out.println("Provider.UserController.findAll()");
        Collection<User> Collection=users.values();
        return ResponseResult.success("OK-7001",Collection);
    }

}
